package com.tangchao.client.sdh.view.card
{
	import com.tangchao.client.sdh.sdhinterface.Out;
	
	import flash.display.MovieClip;

	public class CardGrid extends MovieClip
	{
		private var _cardArray:Array=[];
		private var _cardIntArr:Array;
		private var _defaultOutCardArr:Array = [];
		
		public function CardGrid()
		{
			addChild(new ColorCaohua);
		}
		
		/*
		*设置用户的所有牌
		*/
		public function create(arr:Array)
		{
			destroy();
			this.alpha = 1;
			_cardIntArr = arr;
			if(_cardIntArr)
			{
				var i:int=0;
				for(i=0;i<_cardIntArr.length;i++)
				{
					var card:CardCell=new CardCell();
					card.parentUI = this;
					card.index=_cardIntArr[i];
					card.x=i*20;
					addChild(card);
					_cardArray.push(card);
				}
			}
		}
		
		public function delCards(cardArr:Array):void
		{
			var i:int;
			var j:int;
			var val1:int;
			var val2:int;
			var newCardArr:Array = [];
			var delFlag:Boolean = false;
			
			if(cardArr && _cardIntArr) 
			{
				for(i=0; i<_cardIntArr.length; i++)
				{
					val1 = _cardIntArr[i] as int;
					delFlag = false
					for(j=0; j<cardArr.length; j++)
					{
						val2 = cardArr[j] as int;
						if(val1 == val2)
						{
							delFlag = true;
						}
					}
					
					if(!delFlag)
					{
						newCardArr.push(val1);
					}
				}
			}
			
			_cardIntArr = newCardArr;
			create(_cardIntArr);
		}
		
		/*
		*删除用户的某张牌
		*/
		public function destroyCardByIndex(index:int)
		{
			var i:int=0;
			for(i=0;i<_cardArray.length;i++)
			{
				var card:CardCell=_cardArray[i] as CardCell;
				if(card && card.index==index)
				{
					card.destroy();
					card=null;
					_cardArray.splice(i,1);
					break;
				}
			}
		}
		
		/*
		*获取用户当前选择出的牌
		*/
		public function get nowSelectCard():Array
		{
			var cardIndexArray:Array=[];
			var i:int=0;
			
			for(i=0;i<_cardArray.length;i++)
			{
				var card:CardCell=_cardArray[i] as CardCell;
				if(card && card.select)
				{
					cardIndexArray.push(card.index);
				}
			}
			
			return cardIndexArray;
		}
		
		public function onCilckCard(isSelect:Boolean):void
		{
			Out.getInstance().onClickCard(nowSelectCard, isSelect);
		}
		
		/*
		*提示用户出牌时，调用此接口，array内每个值均为代表牌的数值
		*/
		public function set selectCard(arr:Array)
		{
			var card:CardCell;
			for(i=0;i<_cardArray.length;i++)
			{
				var card =_cardArray[i] as CardCell;
				card.select=false;
			}
			
			var i:int=0;
			var j:int=0;
			
			if(arr)
			{
				for(j=0;j<arr.length;j++)
				{
					for(i=0;i<_cardArray.length;i++)
					{
						var card =_cardArray[i] as CardCell;
						if(card.select==false && card.index==arr[i])
						{
							card.select=true;
							break;
						}
					}
				}
			}
		}
		
		/*
		*删除用户的所有牌
		*/
		public function destroy()
		{
			var i:int=0;
			for(i=0;i<_cardArray.length;i++)
			{
				var card:CardCell=_cardArray[i] as CardCell;
				if(card)
				{
					card.destroy();
					card=null;
				}
			}
			_cardArray.length=0;
			_cardIntArr = null;
		}

	}
}